package net.siufung.boot.swagger.autoconfigure;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.boot.swagger.properties.SwaggerProperties;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;

/**
 * SwaggerAutoConfig
 * @author SIUFUNG
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({SwaggerProperties.class})
@ConditionalOnProperty(prefix=SwaggerProperties.SWAGGER_PREFIX ,name = "enable", havingValue = "true")
@ConditionalOnExpression
@AllArgsConstructor
public class SwaggerAutoConfig {

    private final SwaggerProperties swaggerProperties;

    /**
     * API 页面展示信息
     */
    @Bean(name = "apiInfo")
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                .contact(new Contact(swaggerProperties.getContact(), "", "chenjianfeng@haier.com"))
                .version(StrUtil.concat(false,
                        "Application Version: ",
                        swaggerProperties.getVersion(),
                        ", Spring Boot Version: ",
                        SpringBootVersion.getVersion()))
                .build();
    }

}
